using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Models;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.Interfaces.Filtron;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.Models.DAO;

namespace FiltronASP.Helpers.LastSearches
{

    public class LastSearchesHelper
    {

        public static string makeLastSearch(int makeId, int modelId, int engineId, int filterId, string filterProducer, string languageName) {
            if (!(makeId > 0))
            {
                return String.Empty;
            }
            if (!(modelId > 0))
            {
                return String.Empty;
            } 
            if (!(engineId > 0)) {
                return String.Empty;
            } 
            if (!(filterId > 0)) {
                return String.Empty;
            }
            if (filterProducer.Equals(String.Empty)) {
                return String.Empty;
            }
            if (languageName.Equals(String.Empty)) {
                return String.Empty;
            }

            switch (filterProducer) {
                case "f":
                    makeLastSearchFiltron(makeId, modelId, engineId, filterId, languageName);
                    break;
                case "w":
                    makeLastSearchWix(makeId, modelId, engineId, filterId, languageName);
                    break;
            }

            return String.Empty;

        }

        private static void makeLastSearchFiltron(int makeId, int modelId, int engineId, int filterId, string languageName)
        {
            IFiltronMakeDAO filtronMakeDAO = new FiltronMakeDAO();
            IFiltronModelDAO filtronModelDAO = new FiltronModelDAO();
            IFiltronEngineDAO filtronEngineDAO = new FiltronEngineDAO();
            IFiltronFilterDAO filtronFilterDAO = new FiltronFilterDAO();
            ILastSearchesDAO lastSearchesDAO = new LastSearchesDAO();

            string makeName = filtronMakeDAO.FindByPK(makeId).make_name;
            string modelName = filtronModelDAO.FindByPK(modelId).model_name;
            string engineName = filtronEngineDAO.FindByPK(engineId).engine_name;
            int filterStyleId = Convert.ToInt32(filtronFilterDAO.FindByPK(filterId).filter_style_id);
            string filterType = getFiltronType(filterStyleId, languageName);
            string lastSearchLink = makeFiltronLink(makeName, modelName, filterType, languageName, filterId);

            LastSearch lastSearchToInsert = new LastSearch();
            lastSearchToInsert.last_search_link = lastSearchLink;
            lastSearchToInsert.last_search_name = String.Format("{0} {1} {3} {2}", makeName, modelName, filterType, Helpers.Translations.translationHelper.getTranslation("_filter", languageName));
            lastSearchToInsert.language_id = Translations.translationHelper.getLanguageId(languageName);
            lastSearchesDAO.Add(lastSearchToInsert);
        }

        private static string makeFiltronLink(string makeName, string modelName, string filterType, string languageName, int filterId) {
            return String.Format("?make={0}&model={1}&type={2}&languageName={3}&fid={4}&fProd=F", makeName, modelName, filterType, languageName, filterId);
        }

        private static string makeWixLink(string makeName, string modelName, string filterType, string languageName, int filterId)
        {
            return String.Format("?make={0}&model={1}&type={2}&languageName={3}&fid={4}&fProd=W", makeName, modelName, filterType, languageName, filterId);
        }


        private static string getFiltronType(int filterType, string languageName) {
            switch (filterType)
            {
                case 1:
                    return Helpers.Translations.translationHelper.getTranslation("_oilFilter", languageName);
                case 2:
                    return Helpers.Translations.translationHelper.getTranslation("_airFilter", languageName);
                case 3:
                    return Helpers.Translations.translationHelper.getTranslation("_fuelFilter", languageName);
                case 4:
                    return Helpers.Translations.translationHelper.getTranslation("_cabinFilter", languageName);
            }
            return String.Empty;
        }

        private static string getWixType(int filterType, string languageName)
        {
            switch (filterType)
            {
                case 40:
                    return Helpers.Translations.translationHelper.getTranslation("_oilFilter", languageName);
                case 48:
                    return Helpers.Translations.translationHelper.getTranslation("_airFilter", languageName);
                case 47:
                    return Helpers.Translations.translationHelper.getTranslation("_fuelFilter", languageName);
                case 52:
                    return Helpers.Translations.translationHelper.getTranslation("_cabinFilter", languageName);
            }
            return String.Empty;
        }

        private static void makeLastSearchWix(int makeId, int modelId, int engineId, int filterId, string languageName)
        {
            IWixMakeDAO WixMakeDAO = new WixMakeDAO();
            IWixModelDAO WixModelDAO = new WixModelDAO();
            IWixEngineDAO WixEngineDAO = new WixEngineDAO();
            IWixFilterDAO WixFilterDAO = new WixFilterDAO();
            ILastSearchesDAO lastSearchesDAO = new LastSearchesDAO();

            string makeName = WixMakeDAO.FindByPK(makeId).make_name;
            string modelName = WixModelDAO.FindByPK(modelId).model_name;
            string engineName = WixEngineDAO.FindByPK(engineId).engine_name;
            int filterStyleId = Convert.ToInt32(WixFilterDAO.FindByPK(filterId).filter_type_id);
            string filterType = getWixType(filterStyleId, languageName);
            string lastSearchLink = makeWixLink(makeName, modelName, filterType, languageName, filterId);

            LastSearch lastSearchToInsert = new LastSearch();
            lastSearchToInsert.last_search_link = lastSearchLink;
            lastSearchToInsert.last_search_name = String.Format("{0} {1} {3} {2}", makeName, modelName, filterType, Helpers.Translations.translationHelper.getTranslation("_filter", languageName));
            lastSearchToInsert.language_id = Translations.translationHelper.getLanguageId(languageName);
            lastSearchesDAO.Add(lastSearchToInsert);
        }

    }
}
